/**
 * @file sequention_queue.cpp
 * @brief 顺序队列
 * @author Xavier ZXY (zxy_xavier@163.com)
 * @version : 1.0
 * @date 2022-05-01
 * 
 */

#include <iostream>
#include <stdio.h>
#include "sequention_queue.h"

using namespace std;

int main(int argc, char const *argv[])
{
    Status status;
    int i = 0;
    int l = 0;
    QElemType d = 0;
    SqQueue Q;
    init_queue(&Q);
	printf("初始化队列后，队列空否？%u(1:空 0:否)\n", is_empty_queue(Q));

    printf("请输入整型队列元素不超过 %d 个， -1为结束符", MAXSIZE - 1);
    do
    {
        d = i + 100;
        if (d == -1)
            break;
        i ++;
        insert_tail(&Q, d);
    } while (i < MAXSIZE -1);

    printf("队列长度为: %d\n",get_length(Q));
	printf("现在队列空否？%u(1:空 0:否)\n",is_empty_queue(Q));
	printf("连续%d次由队头删除元素,队尾插入元素:\n",MAXSIZE);

    for(l=1;l<=MAXSIZE;l++)
	{
		delete_head(&Q,&d);
		printf("删除的元素是%d,插入的元素:%d \n",d,l+1000);
		/* scanf("%d",&d); */
		d=l+1000;
		insert_tail(&Q,d);
	}

	l=get_length(Q);
    printf("现在队列中的元素为: \n");
	traverse_queue(Q);
	printf("共向队尾插入了%d个元素\n",i);
	if(l-2>0)
		printf("现在由队头删除%d个元素:\n",l-2);
	while(get_length(Q)>2)
	{
		delete_head(&Q,&d);
		printf("删除的元素值为%d\n",d);
	}

	status =get_head(Q,&d);
	if(status)
		printf("现在队头元素为: %d\n",d);

	printf("清空队列前, 队列空否？%u(1:空 0:否)\n",is_empty_queue(Q));
	clear_queue(&Q);
	printf("清空队列后, 队列空否？%u(1:空 0:否)\n",is_empty_queue(Q));

    return 0;
}


/**
 * @brief 打印元素
 * @param  c                 
 * @return Status 
 */
Status visit(QElemType c) {
    cout << c << endl;

    return OK;
}

/**
 * @brief 初始化一个空队列
 * @param  Q                 
 * @return Status 
 */
Status init_queue(SqQueue *Q) {
    Q->front = 0;
    Q->rear = 0;
    
    return OK;
}

/**
 * @brief 将队列Q清空
 * @param  Q                 
 * @return Status 
 */
Status clear_queue(SqQueue *Q) {
    Q->front = Q->rear = 0;

    return OK;
}

/**
 * @brief 队列Q是否为空队列
 * @param  Q                 
 * @return Status 
 */
Status is_empty_queue(SqQueue Q) {
    if (Q.front == Q.rear) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}

/**
 * @brief Get the length
 * @param  Q                 
 * @return int 
 */
int get_length(SqQueue Q) {

    return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

/**
 * @brief Get queue header element
 * @param  Q                 
 * @param  e                 
 * @return Status 
 */
Status get_head(SqQueue Q, QElemType *e) {
    if (Q.front == Q.rear) {
        return FALSE;
    }
    *e = Q.data[Q.front];
    
    return OK;
}

/**
 * @brief if the queue is not full, insert a new tail element e
 * @param  Q                 
 * @param  e                 
 * @return Status 
 */
Status insert_tail(SqQueue *Q, QElemType e) {
    if ((Q->rear + 1) % MAXSIZE == Q->front)
        return ERROR;
    Q->data[Q->rear] = e;
    Q->rear = (Q->rear + 1) % MAXSIZE;

    return OK;
}

/**
 * @brief if the queue is not empty, delete the header element and
 *          return its value with e
 * @param  Q                 
 * @param  e                 
 * @return Status 
 */
Status delete_head(SqQueue *Q, QElemType *e) {
    if (Q->front == Q->rear)
        return ERROR;
    *e = Q->data[Q->front];
    Q->front = (Q->front + 1) % MAXSIZE;

    return OK;
}

/**
 * @brief Traverse all elements in the queue
 * @param  Q                 
 * @return Status 
 */
Status traverse_queue(SqQueue Q) {
    for (int i = Q.front; (i + Q.front) != Q.rear; ) {
        visit(Q.data[i]);
        i = (i + 1) %MAXSIZE;
    }

    return OK;
}